﻿using System;
using System.Collections.Generic;
using System.Text;
using FieldDevice.ModBus;
using FieldLib;
namespace FieldDevice.Devices
{
    class DeviceEmaxE3 : Device
    {
        public DeviceEmaxE3(byte address, string name)
            : base(DeviceCategory.EmaxE3,address, name)
        {
            //base.EnrollRead("CB_Tripp");            //1表示断路器跳闸
            //base.EnrollRead("CB_Connected");        //1表示断路器开闸，0表示合闸
            //base.EnrollRead("CB_Open");             //1表示断路器合闸，0表示开闸
            Initialize();
        }
        public DeviceEmaxE3(byte address)
            : base(DeviceCategory.EmaxE3, address)
        {
            Initialize();
        
        }

        protected override byte ModBusReadCommand
        {
            get
            {
                return ModBusFunction.READINPUTREGISTER;//04
            }
        }
        void Initialize()
        {
            base.RequestRegister(Register_CBFlag, 1);
            base.RequestRegister(Register_Ia_Hi, 11);
            base.RequestRegister(Register_EPI_Hi, 8);
        }
        byte[] RegistersValue = new byte[522];

        public const ushort Register_CBFlag = 0x65;         //查看断路器状态

        public const ushort Register_Ia_Hi = 0xCB;          //A相电流的寄存器地址高位
        public const ushort Register_Ia_Low = 0xCC;         //A相电流的寄存器地址低位
        public const ushort Register_Ib_Hi = 0xCD;          //B相电流的寄存器地址高位
        public const ushort Register_Ib_Low = 0xCE;         //B相电流的寄存器起始地址低位
        public const ushort Register_Ic_Hi = 0xCF;          //C相电流的寄存器地址高位
        public const ushort Register_Ic_Low = 0xD0;         //C相电流的寄存器地址低位
        public const ushort Register_In_Hi = 0xD1;          //N相电流高位
        public const ushort Register_In_Low = 0xD2;         //N相电流低位
        public const ushort Register_EPI_Hi = 0xfc;         //有功电度高位
        public const ushort Register_EPI_Low = 0xfd;        //有功电度低位
        public const ushort Register_EQL_Hi = 0x0102;       //无功电度高位
        public const ushort Register_EQL_Low = 0x0103;      //无功电度低位

        private uint IA = 0;
        private uint IB = 0;
        private uint IC = 0;
        //private uint In = 0;
        private uint EPI = 0;
        private uint EQL = 0;


        public override void DebugReads()
        {
            if (DeviceManager.GlobalDeviceManager.ModBusIO.IsEmulation)
                this.UpdateReads();
            else
                base.DebugReads();
        }
        public void UpdateReads()
        {
            base.ReadAndUpdateValue(Register_CBFlag, 1);
            base.ReadAndUpdateValue(Register_Ia_Hi, 11);
            base.ReadAndUpdateValue(Register_EPI_Hi, 8);
        }

         DateTime PreviousRecordTime;
        public override void TranslateRegister(FieldLib.ModBusPacket request, byte[] response)
        {
            base.TranslateRegister(request, response);
            DateTime now = DateTime.Now;
            int passedminute = ((now.Second + 60) - PreviousRecordTime.Second) % 60;
            if (passedminute > 1)
            {
                base.PowerQueues.RecordPowerItem(now, CurrentReads["EPI"], CurrentReads["EQL"], CurrentReads["PF"], request.ToBytes(), response);
                this.PreviousRecordTime = now;
            }
        }

        protected override float Translate(byte[] value, int begin, ushort registeraddress)
        {
            ushort tmpValue = value[begin];
            tmpValue <<=8;
            tmpValue+=value[begin+1];
            switch (registeraddress)
            { 

                case Register_CBFlag:
                    //if ((tmpValue & 0x0004) != 0)
                    //    RecordRead("CB_Tripp", 1);
                    //else
                    //    RecordRead("CB_Tripp", 0);

                    if ((tmpValue & 0x0008) != 0)               //1时断路器合闸
                        RecordRead("CB_Connected", 1);      
                    else
                        RecordRead("CB_Connected", 0);
                    if ((tmpValue & 0x0010) != 0)
                        RecordRead("CB_Open", 1);
                    else
                        RecordRead("CB_Open", 0);
                    break;
                case Register_Ia_Hi:
                    IA =tmpValue;
                    //IA <<= 16;
                    RecordRead("IA", IA);
                    break;
                //case Register_Ia_Low:
                //    IA += tmpValue;
                //    RecordRead("IA", IA);
                //    break;
                case Register_Ib_Hi:
                    IB = tmpValue;
                    //IB <<= 16;
                    RecordRead("IB", IB);
                    break;
                //case Register_Ib_Low:
                //    IB += tmpValue;
                //    RecordRead("IB", IB);
                //    break;
                case Register_Ic_Hi:
                    IC = tmpValue;
                    //IC <<= 16;
                    RecordRead("IC", IC);
                    break;
                //case Register_Ic_Low:
                //    RecordRead("IC", IC);
                //    break;
                case Register_EPI_Hi:
                    EPI = tmpValue;
                    EPI <<= 16;
                    break;
                case Register_EPI_Low:
                    EPI += tmpValue;
                    RecordRead("EPI", EPI/1000.0f);
                    break;
                case Register_EQL_Hi:
                    EQL = tmpValue;
                    EQL <<= 16;
                    break;
                case Register_EQL_Low:
                    EQL += tmpValue;
                    RecordRead("EQL", EQL/1000.0f);
                    break;
                default:
                    break;

            }
            return 0.0f;
        }

    }
}
